package com.walking.manager;

import com.walking.entity.ActivityInitiatorEntity;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.Random;

public class ActivityInitiatorManager {
    private double[] mew_md = {0.01, 0.25};
    private double[] old_md = {0.00, 0.01};

    private ActivityInitiatorInfo activityInitiatorInfo;

    public ActivityInitiatorManager(ActivityInitiatorEntity activityInitiatorEntity) {
        this.activityInitiatorInfo = convert(activityInitiatorEntity);
    }

    private ActivityInitiatorInfo convert(ActivityInitiatorEntity activityInitiatorEntity) {
        ActivityInitiatorInfo activityInitiatorInfo = new ActivityInitiatorInfo();
        activityInitiatorInfo.setCurRedValue(activityInitiatorEntity.getCurRedValue());
        BeanUtils.copyProperties(activityInitiatorEntity, activityInitiatorInfo);
        activityInitiatorInfo.setActivityInitiatorEntity(activityInitiatorEntity);
        return activityInitiatorInfo;
    }

    @Data
    public class ActivityInitiatorInfo {
        private ActivityInitiatorEntity activityInitiatorEntity;
        private int newPerson = 0;//新人数量
        private int oldPerson = 0;//老人数量
        private double curRedValue = 95;
        private double redMoney = 100d;//红包上限
        private int status = 0;//0-进行中 1-满百 2-兑换完成
    }

    public boolean assist(boolean isOld) {
        double redVal = 0d;
        if (isOld) {
            int oldPerson = activityInitiatorInfo.getOldPerson();
            if (oldPerson < 5) {
                redVal = getOldPersonRedVal();
            } else {
                redVal = 0d;
            }
            oldPerson++;
        } else {
            redVal = getNewPersonRedVal();
            int newPerson = activityInitiatorInfo.getNewPerson();
            newPerson++;
        }
        double redMoney = activityInitiatorInfo.getCurRedValue();
        redMoney += redVal;
        activityInitiatorInfo.getActivityInitiatorEntity().setCurRedValue(redMoney);
        return false;
    }

    private double getOldPersonRedVal() {
        double randomDouble = getRandomDouble(old_md[0], old_md[1]);
        return randomDouble;
    }

    private double getNewPersonRedVal() {
        double money = 0;
        if (activityInitiatorInfo.getNewPerson() == 19) {
            activityInitiatorInfo.setStatus(1);//修改状态
            return activityInitiatorInfo.getRedMoney() - activityInitiatorInfo.getCurRedValue();
        }
        do {
            money = getRandomDouble(mew_md[0], mew_md[1]);
        } while (activityInitiatorInfo.getCurRedValue() + money > activityInitiatorInfo.getRedMoney());
        return money;
    }

    static double getRandomDouble(double min, double max) {
        //转整数字符串
        String s = max * 100 + "";
        String substring = s.substring(0, s.indexOf("."));
        //转整数
        int bound = Integer.parseInt(substring);
        //随机
        int x = new Random().nextInt(bound);
        double val = 0.01d;
        if (x != 0) {
            //转小数
            val = (double) x / 100;
        }
        if (val < min) {
            return getRandomDouble(min, max);
        }
        return val;
    }
}
